#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "makeargv.c"

//for 'status' variable:
#define INELIGIBLE 0
#define READY 1
#define RUNNING 2
#define FINISHED 3

typedef struct node {
	int id; // corresponds to line number in graph text file
	char prog[1024]; // prog + arguments
	char input[1024]; // filename
	char output[1024]; // filename
	int children[10]; // children IDs
	int num_children; // how many children this node has
	pid_t pid; // track it when it's running
	
	int isPointedTo;
	int hasExecuted;
} node_t;

int main(int argc, char* argv[]) {

	int nbytes = 1024;
	char* fileName = NULL;
	FILE* fd;
	//char* delimiter = ":";
	//char* save;
	char inputString[1024];
	char** myargv;
	
	/* Initializing maximum number of nodes */
	node_t nodeList[50];
	int currentNode;
	
	if (argv[1] != NULL)
		fileName = argv[1];
	
	fd = fopen(fileName,"r");
		if (fd != NULL) {
		
			int currentLine = 0;
			//save = (char *)malloc(1024); //Biggest string is of size 1024
			
			while(fgets(inputString, nbytes, fd) != NULL){
				makeargv(inputString, ":", &myargv);
				
				currentNode = currentLine;
				
				nodeList[currentNode].id = currentLine;
				strcpy( nodeList[currentNode].prog, myargv[0] );
				//strcpy( nodeList[currentNode].children, myargv[1] );
				strcpy( nodeList[currentNode].input, myargv[2] );
				strcpy( nodeList[currentNode].output, myargv[3] );
				
				//edits string input from graphfile and assigns it to node[n].children
				makeargv(myargv[1], " ", &myargv);
				int charCount = 0;
				while( myargv[charCount] != NULL ) {
					if (1 == strlen(myargv[charCount])) {
						strcat( nodeList[currentNode].children,  myargv[charCount]);
						
						nodeList[atoi(myargv[charCount])].isPointedTo = 1;
					}
					else;
					
					charCount++;
				}
				
				nodeList[currentNode].num_children = strlen( nodeList[currentNode].children );
				nodeList[currentNode].hasExecuted = 0;
				
				
				//printf("%s \n", nodeList[currentNode].children); //checks children node string
				//printf("%d \n", nodeList[currentNode].num_children); //checks number of children in each node
				//printf("%d \n", nodeList[currentNode].isPointedTo); //checks if a node is pointing to another node
				
				currentLine++;
				
			}
			
			
			//check for nodes to execute
			/*
			int loopOriginNode = 0;
			int doneExecuting = 0;
			while (0 == doneExecuting) {
			*/
			/*
				int loopNode_execute = 0;
				//executes all nodes waiting to be executed
				while (nodeList[loopNode_execute].prog != NULL) {
			
					
					if (0 == nodeList[loopNode_execute].isPointedTo)
						if (0 == nodeList[loopNode_execute].hasExecuted);
							//executeNode(nodeList[loopNode_execute]);
					else;
					
					
					//printf("%d", nodeList[loopNode_execute].hasExecuted);
					
					loopNode_execute++;
				}
			*/
			
			
				int loopNode_pointer = 0;
				//sets all nodes that are being pointed to
				while (nodeList[loopNode_pointer].children != NULL) {
					//if (0 == nodeList[loopNode_pointer].hasExecuted) {
						int currentChild = 0;
						while (currentChild <= nodeList[loopNode_pointer].num_children) {
							//nodeList[nodeList[loopNode_pointer].children[currentChild]].isPointedTo = 1;
							currentChild++;
						}
						
						//printf("%s", nodeList[loopNode_pointer].children[currentChild]);
					//}
					//else;
					
					loopNode_pointer++;
				}
				
				/*
				int nodesFinished = 0;
				int loopNode_checkExecute = 0;
				while (nodeList[loopNode_checkExecute].prog != NULL) {
					if (0 == nodeList[loopNode_checkExecute].hasExecuted) {
						doneExecuting = 0;
						break;
					}
					else
						doneExecuting = 1;
				}
				*/
			//}
			
		}
		else
			perror(fileName);
	
	return 0;
}
